In [1]:
%matplotlib inline
from ggplot import *
import pandas as pd
import numpy as np
ggplot allows you to adjust both the x and y axis to use a logarithmic scale. The scale_x_log or scale_y_log can be added to any plot. You can also adjust the type of logarithm that is used by providing a base parameter (i.e. scale_x_log(base=2) for natural log) to the function. If not specified log base 10 will be used.
In [2]:
diamonds.head()
Out[2]:
In [3]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point()
Out[3]:
In [4]:
ggplot(diamonds, aes(x='carat', y='price')) + \
geom_point() + \
scale_y_log()
Out[4]:
In [ ]:
In [5]:
df = pd.DataFrame(dict(
x=np.arange(1, 1000)
))
df['y'] = np.log(df.x)
df.head()
Out[5]:
In [6]:
ggplot(df, aes(x='x', y='y')) + geom_point()
Out[6]:
In [7]:
ggplot(df, aes(x='x', y='y')) + geom_point() + scale_x_log()
Out[7]:
In [8]:
ggplot(mtcars, aes(x='mpg')) + geom_histogram() + scale_x_reverse()
Out[8]:
In [9]:
ggplot(mtcars, aes(x='mpg')) + geom_histogram() + scale_y_reverse()
Out[9]:
You can switch between different coordinate systems using the coord_* family of layers. Just be careful that you're using the correct aesthetics! The available coordinate systems are:
coord_equalcoord_flipcoord_polarcoord_cartesian (this is the default, so you never explicitly invoke it)
In [10]:
ggplot(aes(x='beef', y='pork'), data=meat) + \
geom_point() + \
coord_equal()
Out[10]:
In [11]:
ggplot(aes(x='beef', y='pork'), data=meat) + \
geom_point() + \
geom_abline(slope=1, intercept=0, color='teal') + \
coord_equal()
Out[11]:
In [12]:
# sadly, this doesn't appear to work
ggplot(aes(x='beef', y='pork'), data=meat) + \
geom_point() + \
coord_flip()
Out[12]:
In [13]:
df = pd.DataFrame({"x": np.arange(100)})
df['y'] = df.x * 10
df['z'] = ["a" if x%2==0 else "b" for x in df.x]
# polar coords
p = ggplot(df, aes(x='x', y='y')) + geom_point() + coord_polar()
print p
In [14]:
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_line() + coord_polar()
Out[14]:
In [ ]: